import $ from "jquery";

$(document).ready(function () {
  function csh() {
    var timer; // 定时器
    var isLoad = true; // 控制变量
    // 判断图片加载状况，加载完成后回调
    isImgLoad(function () {
      //图片加载完成，想做什么就做什么
      var h_honor_picH = $(
        ".idx_about_top .slideGroup1 ul li .pic a img"
      ).height();
      $(".idx_about_top .slideGroup1 ul li .pic").height(h_honor_picH);

      var h_tjcp_picH = $(".h_tjcp ul li .pro_img a img").height();
      $(".h_tjcp ul li .pro_img a").height(h_tjcp_picH);

      var ny_prolist_picH = $(".ny_prolist ul li .pro_img a img").height();
      $(".ny_prolist ul li .pro_img a").height(ny_prolist_picH);

      var ny_prolist_picH = $(".ny_xgcp ul li .pro_img a img").height();
      $(".ny_xgcp ul li .pro_img a").height(ny_prolist_picH);
    });
    // 判断图片加载的函数
    function isImgLoad(callback) {
      if (
        $(".h_tjcp ul li .pro_img a img").height() === 0 ||
        $(".ny_prolist ul li .pro_img a img").height() === 0 ||
        $(".ny_xgcp ul li .pro_img a img").height() === 0 ||
        $(".h_tjpro .slideGroup4 .parBd ul li .pic img").height() === 0 ||
        $(".idx_about_top .slideGroup1 .parBd ul li .pic img").height() === 0
      ) {
        isLoad = false;
      }
      if (isLoad) {
        // 为true，不为0，加载完毕
        clearTimeout(timer); // 清除定时器
        callback(); // 回调函数
      } else {
        // 为false，没有加载完成，调用定时器递归
        isLoad = true;
        timer = setTimeout(function () {
          isImgLoad(callback); // 递归扫描
        }, 500);
      }
    }

    if (matchMedia("only screen and (min-width:767px)").matches) {
      $("header .navbar_bd").show();
    } else {
      $("header .navbar_bd").hide();
    }

    /*内页产品详情页的产品图片切换*/
    var galleryTop = new Swiper(".pro_qiehuan .gallery-top", {
      nextButton: ".pro_qiehuan  .swiper-button-next",
      prevButton: ".pro_qiehuan  .swiper-button-prev",
      spaceBetween: 0,
    });
    var galleryThumbs = new Swiper(".pro_qiehuan  .gallery-thumbs", {
      spaceBetween: 10,
      centeredSlides: true,
      slidesPerView: "auto",
      touchRatio: 0.2,
      slideToClickedSlide: true,
    });
    galleryTop.params.control = galleryThumbs;
    galleryThumbs.params.control = galleryTop;
  }
  csh();
  $(window).resize(function () {
    csh();
  });
});

/*产品目录*/
$(".pro_mulu_wrap .pro_mulu li p").click(function () {
  $(this).parents("li").children("div").toggle();
  $(this).toggleClass("on");
});
$(".pro_mulu_wrap .pro_mulu li p a").click(function (event) {
  event.stopPropagation();
});

/*首页新闻tab切换*/
$(".h_news .news_hd span").click(function () {
  var idx_news_num1 = $(this).index();
  $(this).addClass("on").siblings("span").removeClass("on");
  $(".h_news .news_bd>div").eq(idx_news_num1).show().siblings("div").hide();
});

/*手机端产品目录展开*/
var menu_tit_flag = 0;
$(".pro_mulu_wrap .c_tlt").on("click", function () {
  if (matchMedia("only screen and (max-width:767px)").matches) {
    if (menu_tit_flag == 0) {
      $(".pro_mulu_wrap .pro_mulu").stop(true).slideToggle(300);
      menu_tit_flag = 1;
    } else {
      $(".pro_mulu_wrap .pro_mulu").stop(true).slideToggle(300);
      menu_tit_flag = 0;
    }
  }
});

/*产品详情页点击tab切换*/
$(".nyprodetail2 .hd span").on("click", function () {
  var num1 = $(this).index();
  $(".nyprodetail2 .bd section")
    .eq(num1)
    .show()
    .siblings(".nyprodetail2 .bd section")
    .hide();
  $(this).addClass("on").siblings(".nyprodetail2 .hd span").removeClass("on");
});

$(".orderPro").on("click", function () {
  var num1 = 1;
  $(".nyprodetail2 .bd section")
    .eq(num1)
    .show()
    .siblings(".nyprodetail2 .bd section")
    .hide();
  $(".nyprodetail2 .hd span")
    .eq(1)
    .addClass("on")
    .siblings(".nyprodetail2 .hd span")
    .removeClass("on");
});

/*首页滚动 begin*/
/*分多种情况，一个是一行显示3个(.slideGroup)，一个是一行显示4个(.slideGroup4)，一个是一行显示2个(.slideGroup2)*/
$(document).ready(function () {
  $(window).resize(function () {
    //执行代码块
    MenuHeight1();
    MenuHeight3();
    MenuHeight4();
    MenuHeight5();
    MenuHeight10();
  });
  MenuHeight1();
  MenuHeight3();
  MenuHeight4();
  MenuHeight5();
  MenuHeight10();

  function MenuHeight1() {
    var swiper1 = new Swiper(".slideGroup1 .swiper-container", {
      pagination: ".swiper-pagination",
      nextButton: ".sNext",
      prevButton: ".sPrev",
      slidesPerView: 1,
      paginationClickable: true,
      loop: true,
      autoplay: 3000,
    });
  }

  function MenuHeight3() {
    var w = $(window).width();
    var vis = 3;
    if (w >= 1200) {
      vis = 3;
    } else if (w <= 1199 && w > 880) {
      vis = 3;
    } else if (w <= 880 && w > 380) {
      vis = 2;
    } else if (w <= 380) {
      vis = 1;
    }
    var pro_bdW = $(".slideGroup .parBd").width();
    var pro_LiW = (pro_bdW - 36 * vis) / vis;
    if (w <= 380) {
      var pro_LiW = (pro_bdW - 18 * vis) / vis;
    }
    $(".slideGroup .parBd ul li").width(pro_LiW);

    var picH = $(".slideGroup .parBd ul li .pic img").outerHeight();
    $(".slideGroup .parBd ul li .pic").height(picH);

    var swiper3 = new Swiper(".slideGroup .swiper-container", {
      slidesPerView: vis,
      spaceBetween: 30,
      pagination: {
        el: ".swiper-pagination",
        clickable: true,
      },
      autoplay: 3000,
    });
  }
  function MenuHeight4() {
    var w = $(window).width();
    var vis = 4;
    if (w >= 1200) {
      vis = 4;
    } else if (w <= 1199 && w > 880) {
      vis = 3;
    } else if (w <= 880 && w > 380) {
      vis = 2;
    } else if (w <= 380) {
      vis = 2;
    }
    var pro_bdW = $(".slideGroup4 .parBd").width();
    var pro_LiW = (pro_bdW - 36 * vis) / vis;
    if (w <= 380) {
      var pro_LiW = (pro_bdW - 18 * vis) / vis;
    }
    $(".slideGroup4 .parBd ul li").width(pro_LiW);

    var swiper4 = new Swiper(".slideGroup4 .swiper-container", {
      slidesPerView: vis,
      spaceBetween: 20,
      pagination: {
        el: ".swiper-pagination",
        clickable: true,
      },
      autoplay: 3000,
    });
  }

  function MenuHeight5() {
    var w = $(window).width();
    var vis = 5;
    if (w >= 1200) {
      vis = 5;
    } else if (w <= 1199 && w > 880) {
      vis = 3;
    } else if (w <= 880 && w > 380) {
      vis = 2;
    } else if (w <= 380) {
      vis = 2;
    }
    var numa = $(".slideGroup5 .parBd ul li").length;
    if (numa <= vis) {
      $(".slideGroup5").addClass("swiper-no-swiping");
    } else {
      $(".slideGroup5").removeClass("swiper-no-swiping");
    }
    var pro_bdW = $(".slideGroup5 .parBd").width();
    var pro_LiW = (pro_bdW - 36 * vis) / vis;
    if (w <= 380) {
      var pro_LiW = (pro_bdW - 18 * vis) / vis;
    }
    $(".slideGroup5 .parBd ul li").width(pro_LiW);

    var swiper5 = new Swiper(".slideGroup5 .swiper-container", {
      slidesPerView: vis,
      spaceBetween: 2,
      pagination: {
        el: ".swiper-pagination",
        clickable: true,
      },
      autoplay: 3000,
    });
  }

  function MenuHeight10() {
    var w = $(window).width();
    var vis = 10;
    if (w >= 1200) {
      vis = 10;
    } else if (w <= 1199 && w > 880) {
      vis = 6;
    } else if (w <= 880 && w > 380) {
      vis = 5;
    } else if (w <= 380) {
      vis = 3;
    }
    var numa = $(".slideGroup10 .parBd ul li").length;
    if (numa < vis) {
      $(".slideGroup10 .parBd ul").css("display", "block");
      $(".slideGroup10").addClass("swiper-no-swiping");
    } else {
      $(".slideGroup10 .parBd ul").css("display", "flex");
      $(".slideGroup10").removeClass("swiper-no-swiping");
    }
    var pro_bdW = $(".slideGroup10 .parBd").width();
    var pro_LiW = (pro_bdW - 36 * vis) / vis;
    if (w <= 380) {
      var pro_LiW = (pro_bdW - 18 * vis) / vis;
    }
    $(".slideGroup10 .parBd ul li").width(pro_LiW);

    var swiper10 = new Swiper(".slideGroup10 .swiper-container", {
      slidesPerView: vis,
      spaceBetween: 15,
      pagination: {
        el: ".swiper-pagination",
        clickable: true,
      },
      autoplay: 3000,
    });
  }
});
/*首页滚动 end*/

/* -----QQ 侧边悬浮 ---- */
var cmstools = {
  get_customers: function (postion) {
    var var_i = 0; //变量说明
    var ook = function (msg) {
      console.log("另外一个" + msg);
    };
  },
  setbanner: function (id, baneffect) {
    var instance = $(id);
  },
  setroll: function (id, speed, isroll) {
    if (typeof isroll == "undefined") {
      isroll = 1;
    }
  },
  setad: function (id, position) {},
  setfloatqq: function (id, position1, position2) {
    var tNode = $("#" + id + "");
    var pos = {};
    pos[position1] = "5px";
    pos[position2] = "40px";

    if (!tNode.get(0)) {
      tNode = $("." + id + "");
    }
    tNode.css(pos).addClass("floatqq_" + position1);

    tNode.find(".offset-top").on("click", function () {
      $("html,body").animate({ scrollTop: $("body").offset().top }, 1000);
    });
  },
};

$(function () {
  $(".suspend-item").hover(
    function () {
      $(this).children().addClass("show_qq");
    },
    function () {
      $(this).children().removeClass("show_qq");
    }
  );
});
$(function () {
  $(".suspend-item-item").hover(
    function () {
      $(this).children().addClass("show_qq");
    },
    function () {
      $(this).children().removeClass("show_qq");
    }
  );
});
$(function () {
  $(".suspend-item").hover(
    function () {
      $(this).children().addClass("show_dh");
    },
    function () {
      $(this).children().removeClass("show_dh");
    }
  );
});
$(function () {
  $(".suspend-item-item").hover(
    function () {
      $(this).children().addClass("show_dh");
    },
    function () {
      $(this).children().removeClass("show_dh");
    }
  );
});

/*手机端返回*/
$(document).ready(function () {
  $(document).on("mouseleave", ".fn-suspend, .fn-suspend .a-top", function () {
    $(".fn-suspend").find(".d").hide();
  });
  $(document).on("mouseenter", ".fn-suspend .a-top", function () {
    $(".fn-suspend").find(".d").hide();
  });
  $(document).on("click", ".fn-suspend .a-top", function () {
    $("html,body").animate({ scrollTop: 0 });
  });
  $(window).scroll(function () {
    var st = $(document).scrollTop();
    var $top = $(".fn-suspend .a-top");
    if (st > 400) {
      $top.css({ display: "block" });
    } else {
      if ($top.is(":visible")) {
        $top.hide();
      }
    }
  });
});
